<?php
session_start();

if (empty($_GET['action'])) {
    $smarty->assign('from_site', $_GET['from_site']);
    $smarty->display('general/login.tpl');
} elseif ($_GET['action'] == 'login') {
    $adminsession = new Adminsession;
    $adminsession->db = $db;
    $login = $adminsession->login($_POST['username'], $_POST['password']);
    if ($login) {
        header('location: ' . HOME_URL . '/index.php?' .
            (strlen($_POST['from_site']) > 0 ? 'site=' . $_POST['from_site'] . '&' : '') . 'login=true');
    } else {
        header('location: ' . HOME_URL . '/index.php?' .
            (strlen($_POST['from_site']) > 0 ? 'site=' . $_POST['from_site'] . '&' : '') . 'login=false');
    }
} elseif ($_GET['action'] == 'logout') {
    $adminsession = new Adminsession;
    $adminsession->db = $db;
    $logout = $adminsession->logout();
    if ($logout) {
        header('location: ' . HOME_URL . '/index.php?' .
            (strlen($_GET['from_site']) > 0 ? 'site=' . $_GET['from_site'] . '&' : '') . 'logout=true');
    } else {
        header('location: ' . HOME_URL . '/index.php?' .
            (strlen($_GET['from_site']) > 0 ? 'site=' . $_GET['from_site'] . '&' : '') . 'logout=false');
    }
}
